home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 027a / vntx.zip / CORRUPT.PRG < prev    next >
Text File  |  1991-02-07  |  3KB  |  99 lines

  1. #include "fileio.ch"
  2. #include "corrupt.ch"
  3.  
  4. static nNtx, nKeySize, bKey, nStatus, nCol, nChecked, nProblemRecno
  5.  
  6. /*--------------------------------CORRUPTED-----------------------------------*/
  7.  
  8. function corrupted(sDbfName, ;  // name of database on which index is based
  9.                    sNtxName, ;  // name of index to check for corruption
  10.                    nRecno)      // record number of offending record, if any
  11.  
  12. local sBuffer, nRootPtr, sKeyExpr := '', sOldScrn, nOldArea := select()
  13. local nOldCoords := {row(), col()}, nOldCursor := setCursor(0)
  14.  
  15. * begin
  16.     if ( (sDbfName == NIL) .or. (sNtxName == NIL) ); return CO_NTXOPEN; end
  17.     if ( !('.' $ sDbfName) ); sDbfName += '.dbf'; end
  18.     if ( !('.' $ sNtxName) ); sNtxName += '.ntx'; end
  19.     if ( !file(sDbfName) ); return CO_BADDBF; end
  20.     nNtx := fopen(sNtxName, FO_READ)
  21.     if ( nNtx < 0 ); return CO_NTXOPEN; end
  22.  
  23.     fSeek(nNtx, 4, FS_SET)                  //read offset to root node
  24.     sBuffer := space(4)
  25.     if ( fRead(nNtx, @sBuffer, 4) < 4 )
  26.         fclose(nNtx)
  27.         return CO_NTXREAD
  28.     end
  29.     nRootPtr := bin2L(sBuffer)
  30.  
  31.     fSeek(nNtx, 14, FS_SET)                 //read key size
  32.     sBuffer := space(4)
  33.     fRead(nNtx, @sBuffer, 4)
  34.     nKeySize := bin2L(sBuffer)
  35.  
  36.     fSeek(nNtx, 22, FS_SET)                 //read key expression and compile
  37.     sBuffer := space(1)
  38.     fRead(nNtx, @sBuffer, 1)
  39.     while ( asc(sBuffer) <> 0 )
  40.         sKeyExpr += sBuffer
  41.         sBuffer := space(1)
  42.         fRead(nNtx, @sBuffer, 1)
  43.     end
  44.     bKey := &('{ || ' + alltrim(sKeyExpr) + ' }')
  45.  
  46.     use (sDbfName) new
  47.     sOldScrn := savescreen(10, 2, 12, 78)
  48.     @ 10,2 clear to 12,78
  49.     @ 10,2 to 12,78
  50.     @ 11,3 say ' Verifying ' + sNtxName + ' of ' + sDbfName + '.           0/' + ;
  51.                str(lastrec(), 10, 0) + ' verified.  '
  52.     nCol := len(sDbfName) + len(sNtxName) + 21
  53.     nChecked := 0
  54.     nProblemRecno := 0
  55.     nStatus := CO_OK
  56.     traverse(nRootPtr)
  57.     nRecno := nProblemRecno
  58.     select (nOldArea)
  59.     restscreen(10, 2, 12, 78, sOldScrn)
  60.     setCursor(nOldCursor)
  61.     @ nOldCoords[1], nOldCoords[2] say ''
  62. return nStatus
  63.  
  64. /*---------------------------------TRAVERSE-----------------------------------*/
  65.  
  66. function traverse(nNodePtr)
  67.  
  68. local sPage := space(1024), nEntryCount, nI := 1, nEntryPtr
  69. local aEntry := { 0, 0, '' }
  70.  
  71. * begin
  72.     if ( nNodePtr == 0 ); return NIL; end
  73.     fSeek(nNtx, nNodePtr, FS_SET)
  74.     fRead(nNtx, @sPage, 1024)
  75.     nEntryCount := bin2I(sPage)
  76.     while ( (nStatus == CO_OK) .and. (nI <= nEntryCount) )
  77.         nChecked++
  78.         @ 11, nCol say str(nChecked, 10, 0)
  79.         getEntry(@sPage, nI, nKeySize, aEntry)
  80.         if ( aEntry[CO_RECNO] > lastrec() )
  81.             nStatus := CO_BADRECNO
  82.             nProblemRecno := aEntry[CO_RECNO]
  83.         else
  84.             goto aEntry[CO_RECNO]
  85.             if ( aEntry[CO_KEY] <> eval(bKey) )
  86.                 nStatus := CO_BADKEY
  87.                 nProblemRecno := aEntry[CO_RECNO]
  88.             else
  89.                 traverse(aEntry[CO_CHILD])
  90.                 nI++
  91.             end
  92.         end
  93.     end
  94.     if ( nStatus == CO_OK )
  95.         getEntry(@sPage, nI, nKeySize, aEntry)
  96.         traverse(aEntry[CO_CHILD])
  97.     end
  98. return NIL
  99.